package cn.mw.cmdb.template.service;

import cn.mw.cmdb.template.constant.MwTemplateConfigurationConstant;
import cn.mw.cmdb.template.enums.ZabbixVersionEnum;
import cn.mw.cmdb.template.param.MacroParam;
import cn.mw.cmdb.template.param.MwTemplateConfigurationParam;
import cn.mw.cmdb.template.param.TemplateParam;
import cn.mw.microMonitorCommon.util.ListMapObjUtils;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @author gengjb
 * @description 模板配置参数管理
 * @date 2024/7/29 16:26
 */
@Component
public class TemplateConfigurationParamManager {

    public Map getAllTemplateParam(MwTemplateConfigurationParam param){
        Map zabbixParam = new HashMap();
        zabbixParam.put(MwTemplateConfigurationConstant.OUTPUT, Arrays.asList(MwTemplateConfigurationConstant.TEMPLATE_ID,MwTemplateConfigurationConstant.NAME));
        zabbixParam.put(MwTemplateConfigurationConstant.selectGroups, Arrays.asList(MwTemplateConfigurationConstant.GROUP_ID,MwTemplateConfigurationConstant.NAME));
//        zabbixParam.put(MwTemplateConfigurationConstant.selectTags, MwTemplateConfigurationConstant.COUNT);
//        zabbixParam.put(MwTemplateConfigurationConstant.selectGraphs, MwTemplateConfigurationConstant.COUNT);
//        zabbixParam.put(MwTemplateConfigurationConstant.selectDashboards, MwTemplateConfigurationConstant.COUNT);
        zabbixParam.put(MwTemplateConfigurationConstant.selectDiscoveries, MwTemplateConfigurationConstant.COUNT);
//        zabbixParam.put(MwTemplateConfigurationConstant.selectHttpTests, MwTemplateConfigurationConstant.COUNT);
//        zabbixParam.put(MwTemplateConfigurationConstant.selectHosts, MwTemplateConfigurationConstant.COUNT);
        zabbixParam.put(MwTemplateConfigurationConstant.selectItems, MwTemplateConfigurationConstant.COUNT);
        zabbixParam.put(MwTemplateConfigurationConstant.selectTemplates, MwTemplateConfigurationConstant.TEMPLATE_ID);
        zabbixParam.put(MwTemplateConfigurationConstant.selectParentTemplates,  Arrays.asList(MwTemplateConfigurationConstant.TEMPLATE_ID));
        zabbixParam.put(MwTemplateConfigurationConstant.selectTriggers,  MwTemplateConfigurationConstant.COUNT);
//        zabbixParam.put(MwTemplateConfigurationConstant.selectApplications,  MwTemplateConfigurationConstant.COUNT);
        if(CollectionUtils.isNotEmpty(param.getTemplateIds())){
            zabbixParam.put(MwTemplateConfigurationConstant.TEMPLATE_IDS, param.getTemplateIds());
        }
        if(StringUtils.isNotEmpty(param.getGroupId())){
            zabbixParam.put(MwTemplateConfigurationConstant.GROUP_IDS, param.getGroupId());
        }
        return zabbixParam;
    }


    public Map createAndEditTemplateParam(TemplateParam templateParam,ZabbixVersionEnum zabbixVersion){
        Map zabbixParam = new HashMap();
        if(StringUtils.isNotEmpty(templateParam.getTemplateId())){
            zabbixParam.put(MwTemplateConfigurationConstant.TEMPLATE_ID,templateParam.getTemplateId());
        }
        zabbixParam.put(MwTemplateConfigurationConstant.TEMPLATE_NAME,templateParam.getHost());
        zabbixParam.put(MwTemplateConfigurationConstant.NAME,templateParam.getTemplateName());
        zabbixParam.put(MwTemplateConfigurationConstant.DESCRIPTION,templateParam.getDescription());
        List<String> templateGroup = templateParam.getTemplateGroup();
        List<Map> groupMap = new ArrayList<>();
        for (String group : templateGroup) {
            Map map = new HashMap();
            map.put(MwTemplateConfigurationConstant.GROUP_ID,group);
            groupMap.add(map);
        }
        zabbixParam.put(MwTemplateConfigurationConstant.GROUPS,groupMap);
        if(CollectionUtils.isNotEmpty(templateParam.getTags())){
            zabbixParam.put(MwTemplateConfigurationConstant.TAGS,templateParam.getTags());
        }
        if(CollectionUtils.isNotEmpty(templateParam.getLinkTemplateIds())){
            switch (zabbixVersion){
                case ZABBIX_5:
                    zabbixParam.put(MwTemplateConfigurationConstant.TEMPLATES,templateParam.getLinkTemplateIds());
                    break;
                default:
                    List<String> linkTemplateIds = templateParam.getLinkTemplateIds();
                    List<Map> linkTemplateMaps = new ArrayList<>();
                    for (String linkTemplateId : linkTemplateIds) {
                        Map map = new HashMap();
                        map.put(MwTemplateConfigurationConstant.TEMPLATE_ID,linkTemplateId);
                        linkTemplateMaps.add(map);
                    }
                    zabbixParam.put(MwTemplateConfigurationConstant.TEMPLATES,linkTemplateMaps);
            }
        }
        List<MacroParam> macroParams = templateParam.getMacroParams();
        if(CollectionUtils.isNotEmpty(macroParams)){
            List<Map> maps = ListMapObjUtils.objectsToMaps(macroParams);
            zabbixParam.put(MwTemplateConfigurationConstant.MACROS,maps);
        }
        return zabbixParam;
    }

    public Map exportQueryParam(List<String> templateIds){
        Map zabbixParam = new HashMap();
        zabbixParam.put(MwTemplateConfigurationConstant.OUTPUT, Arrays.asList(MwTemplateConfigurationConstant.TEMPLATE_ID,MwTemplateConfigurationConstant.NAME));
        zabbixParam.put(MwTemplateConfigurationConstant.TEMPLATE_IDS, templateIds);
        zabbixParam.put(MwTemplateConfigurationConstant.selectHosts, MwTemplateConfigurationConstant.EXTEND);
        zabbixParam.put(MwTemplateConfigurationConstant.selectItems, MwTemplateConfigurationConstant.EXTEND);
        zabbixParam.put(MwTemplateConfigurationConstant.selectParentTemplates, MwTemplateConfigurationConstant.EXTEND);
        zabbixParam.put(MwTemplateConfigurationConstant.selectTriggers,  MwTemplateConfigurationConstant.EXTEND);
        zabbixParam.put(MwTemplateConfigurationConstant.selectDiscoveries, MwTemplateConfigurationConstant.EXTEND);
        return zabbixParam;
    }
}
